package com.hhck.controller;

import com.hhck.entity.InStore;
import com.hhck.entity.Page;
import com.hhck.entity.Result;
import com.hhck.entity.Store;
import com.hhck.service.InStoreService;
import com.hhck.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/instore")
public class InStoreController {

    @Autowired
    private InStoreService inStoreService;

    @Autowired
    private StoreService storeService;

    /**
     * 查询所有仓库
     */
    @GetMapping("/store-list")
    public Result storeList() {
        return Result.ok(storeService.queryAllStore());
    }

    /**
     * 分页查询入库单列表（条件查询）
     */
    @GetMapping("/instore-page-list")
    public Result instorePageList(Page page, InStore inStore) {
        return Result.ok(inStoreService.queryInStoreByPage(page, inStore));
    }

    /**
     * 确定入库
     */
    @PutMapping("/instore-confirm")
    public Result instoreConfirm(@RequestBody InStore inStore) {

        // 确定入库业务，操作两张表，需要开启事务
        try {
            int num = inStoreService.modifyInStoreIsInByInsId(inStore);
            if (num == 2) {
                return Result.ok("入库成功！");
            } else {
                return Result.err(Result.CODE_ERR_BUSINESS, "入库失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.err(Result.CODE_ERR_BUSINESS, "入库失败！");
        }

    }

}

